package me.dr0n3.skyRanks;

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import me.dr0n3.skyRanks.Commands.playedCommand;
import me.dr0n3.skyRanks.Commands.playedTopCommand;
import me.dr0n3.skyRanks.Utils.IOHelper;
import me.dr0n3.skyRanks.Utils.MessageHelper;
import me.dr0n3.skyRanks.Utils.PermissionsHelper;
import me.dr0n3.skyRanks.Utils.TimeHelper;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;

/**
 * @author dr0n3
 * @date 17.02.2012
 */
//lalala
public class skyRanks extends JavaPlugin
{
    // <editor-fold defaultstate="collapsed" desc="Variablen">
    static final Logger log = Logger.getLogger("Minecraft");
    public static List<Player> lBlocked = new LinkedList<>();
    public FileConfiguration config;
    // </editor-fold>

    @Override
    public void onDisable() 
    {
        MessageHelper.writeLog("disabled!", Level.INFO);
    }

    @Override
    public void onEnable() 
    {
        File file = new File(IOHelper.mainDirectory);
        
        // <editor-fold defaultstate="collapsed" desc="Make Directories">
        if(!file.exists())
        {
            try 
            {
                MessageHelper.writeLog("Erstelle Verzeichnisse...", Level.INFO);
                new File(IOHelper.mainDirectory).mkdir();
                new File(IOHelper.mainDirectory + "/data").mkdir();
                new File(IOHelper.mainDirectory + "/config.yml").createNewFile();
                
                getConfig().options().copyDefaults(true);
                getConfig().options().copyHeader(true);
                saveConfig();
            } 
            catch (IOException ex) 
            {
                MessageHelper.writeLog("Konnte Verzeichnisse oder Config nicht erstellen!", Level.SEVERE);
            }
        }
        // </editor-fold>
        
        IOHelper.loadConfig(this);
        setupPermissions();
        
        PluginManager pm = this.getServer().getPluginManager();
        pm.registerEvents(new skyPlayerListener(this), this);
        
        this.getCommand("played").setExecutor(new playedCommand(this));
        this.getCommand("playedtop").setExecutor(new playedTopCommand(this));

        TimeHelper.getLoginTimeOnEnabled(this);
        
        MessageHelper.writeLog("v" + getDescription().getVersion() + " enabled!", Level.INFO);
    }
    
    private boolean setupPermissions()
    {
        RegisteredServiceProvider<Permission> permissionProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.permission.Permission.class);
        if (permissionProvider != null) {
            PermissionsHelper.permission = permissionProvider.getProvider();
        }
        return (PermissionsHelper.permission != null);
    }
    
    
}
